package core

import (
	"errors"
	"sync"
)

var (
	initialized bool
	initMutex   sync.Mutex

	ErrNotInitialized     = errors.New("keyauthcore not initialized")
	ErrAlreadyInitialized = errors.New("keyauthcore already initialized")
)

// Initialize 初始化库
func Initialize() error {
	initMutex.Lock()
	defer initMutex.Unlock()

	if initialized {
		return ErrAlreadyInitialized
	}

	initialized = true
	return nil
}

// Cleanup 清理资源
func Cleanup() error {
	initMutex.Lock()
	defer initMutex.Unlock()

	if !initialized {
		return ErrNotInitialized
	}

	initialized = false
	return nil
}

// IsInitialized 检查是否已初始化
func IsInitialized() bool {
	initMutex.Lock()
	defer initMutex.Unlock()
	return initialized
}

// HandleError 处理错误并记录
func HandleError(err error) error {
	if err != nil {
		LogError(err.Error())
	}
	return err
}
